package cn.kgc.crowdfundingadmin03.controller;

import cn.kgc.crowdfundingadmin03.common.ResultEntity;
import cn.kgc.crowdfundingadmin03.pojo.Menu;
import cn.kgc.crowdfundingadmin03.pojo.Role;
import cn.kgc.crowdfundingadmin03.service.MenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author shkstart
 * @create 2020-02-28 9:03
 */
@RestController
public class MenuController {

    @Resource
    MenuService menuService;

    //查询所有节点
    @RequestMapping("/menu/get/whole/tree")
    public ResultEntity<Menu> getWholeTree(){
        //查询所有的树节点用于组装
        List<Menu> all = menuService.getAll();
        //转成map格式
        Map<Integer,Menu> map=new HashMap<>();
        for (Menu menu : all) {
            map.put(menu.getId(),menu);
        }

        //声明变量存储 跟节点对象
        Menu rootNode=null;

        for (Menu menu : all) {
            //父节点 如果父节点为null则
            if(menu.getPid()==null){
            rootNode=menu;
            continue;
            }
            //是子节点从all找到父节点,并添加到父节点的子节点集合
            //get  //获取指定键所映射的值
            map.get(menu.getPid()).getChildren().add(menu);
        }
        return ResultEntity.successWithoutData(rootNode);
    }
    @RequestMapping("/menu/save")
    public ResultEntity<String> saveMenu(Menu menu) {

        menuService.saveMenu(menu);

        return ResultEntity.successWithoutData();
    }

    @RequestMapping("/menu/get/{menuId}")
    public ResultEntity<Menu> getMenuById(@PathVariable("menuId") Integer menuId) {

        Menu menu = menuService.getMenuById(menuId);

        return ResultEntity.successWithoutData(menu);
    }

    /*修改数据*/
    @RequestMapping("/menu/update")
    public ResultEntity<String> update(Menu menu){
        menuService.update(menu);
        return ResultEntity.successWithoutData();
    }



    @RequestMapping("/menu/batch/remove/{roleIdList}")
    public ResultEntity<String> batchRemove(@PathVariable Integer roleIdList) {
       ;
        menuService.batchRemove(roleIdList);
        return ResultEntity.successWithoutData();
    }

}
